#include <stdio.h>
#include <algorithm>
#define THREADS_PER_BLOCK 16
#define DIVUP(m, n) ((m) / (n) + ((m) % (n) > 0))

// 定义cuda核函数
__global__ void test_core(const float* const data1_ptr,
    const float* const data2_ptr,
    float* const data_ptr,
    const int rows,
    const int cols){
    const int row = blockIdx.x * THREADS_PER_BLOCK + threadIdx.x;
    const int col = blockIdx.y * THREADS_PER_BLOCK + threadIdx.y;
    if(row>=rows||col>=cols)
        return;
    *(data_ptr+row*cols+col) = *(data1_ptr+row*cols+col) + *(data2_ptr+row*cols+col);
}

// 用于调用cuda核函数
void test_launch(const float* const data1_ptr,
    const float* const data2_ptr,
    float* const data_ptr,
    const int rows,
    const int cols){
    dim3 blocks(DIVUP(rows,THREADS_PER_BLOCK),DIVUP(cols,THREADS_PER_BLOCK));
    dim3 threads(THREADS_PER_BLOCK,THREADS_PER_BLOCK);
    test_core<<<blocks,threads>>>(data1_ptr,data2_ptr, data_ptr,rows,cols);
    cudaDeviceSynchronize();
}